#!/bin/sh

## Tests for x86 architecture are run as kernel images for qemu
## that supports multiboot format. Tests uses an infrastructure
## called from the bios code. The infrastructure initialize the
## system/cpu's, switch to long-mode and calls the 'main' function
## of the individual test. Tests uses a qemu's virtual test device,
## named testdev, for services like printing, exiting, query memory size etc.

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/upload.sh
. $LKP_SRC/lib/tests/kvm-unit-tests.sh
. $LKP_SRC/lib/reproduce-log.sh

cd $BENCHMARK_ROOT/kvm-unit-tests || die "no $BENCHMARK_ROOT/kvm-unit-tests"

# Debian/Fedora/Ubuntu use /usr/bin/qemu-system-x86_64 as QEMU Emulator.
# RHEL use /usr/libexec/qemu-kvm as QEMU Emulator.

if qemu-system-x86_64 --help >/dev/null 2>&1; then
	export QEMU="$(which qemu-system-x86_64)"
else
	if [ -f "/usr/libexec/qemu-kvm" ]; then
		export QEMU="/usr/libexec/qemu-kvm"
	else
		die "Can not find QEMU Emulator"
	fi
fi

# errata.txt and run_tests.sh are under same directory, using
# absolute path for errata.sh will increase the risk of missing
# file, here we directly use relative path.
sed -i 's/\(ERRATATXT=\).*/\1errata.txt/' config.mak

# below fix timeout issue in kvm-unit-tests-qemu,
# 'FAIL vmx_pf_exception_test_reduced_maxphyaddr (timeout; duration=90s)'
# we should keep same setting for bare metal
sed -i '/\[vmx_pf_exception_test_reduced_maxphyaddr\]/a\timeout = 240' x86/unittests.cfg

setup_test_environment || die "failed to setup test environment"

check_ignored_cases

run_tests
[ $? -ne 0 ] && dump_qemu

upload_test_results
